import Vue from 'vue'
import Router from 'vue-router'
import App from './app.vue'
import Resource from 'vue-resource'
import {base64,md5} from 'vux'

import Index from './views/index.vue'
import VuxDemo from './views/vux-demo.vue'
import UserRegister1 from './views/user-register1.vue'
import UserRegister2 from './views/user-register2.vue'
import UserRegister3 from './views/user-register3.vue'
import UserLogin from './views/user-login.vue'
import UserInfo from './views/user-info.vue'
import TaskList from './views/task-list.vue'
import TaskView from './views/task-view.vue'
import TaskNew from './views/task-new.vue'
import ProjectList from './views/project-list.vue'
import ProjectView from './views/project-view.vue'
import ProjectNew from './views/project-new.vue'

Vue.use(Router)
Vue.use(Resource)
Vue.config.debug = true 
Vue.config.silent = true

var router = new Router({
    hashbang: false,
    history: false,
    saveScrollPosition: true,
    transitionOnLoad: true
})

router.map({
	'/': {
		component: Index
	},
	'/user.login': {
		component: UserLogin
	},
	'/user.register1': {
		component: UserRegister1
	},
	'/user.register2': {
		component: UserRegister2
	},
	'/user.register3': {
		component: UserRegister3
	},
	'/user.info': {
		component: UserInfo
	}, 
	'/vux-demo': {
		component: VuxDemo
	},
	'/task.list': {
		component: TaskList
	},
	'/task.view?:id': {
		component: TaskView
	},
	'/task.new': {
		component: TaskNew
	},
	'/project.list': {
		component: ProjectList
	},
	'/project.view': {
		component: ProjectView
	},
	'/project.new': {
		component: ProjectNew
	},
})

router.beforeEach(() => {
    var token = localStorage.getItem("token");
    if(token==null || token==""){
        router.go("/user.login");
     }
});

router.afterEach(() =>{});

router.start(App, '#app')

//sui init
$.config = {router: false}
$.init() 

Vue.prototype.ajax = function(url,action,param,callback){
	var self = this;
	this.$http.jsonp({
	   	method:'POST',
		url: url,
		data: {
			'action':action,
			'kwargs':param
		},
		headers: {
			"X-Requested-With": "XMLHttpRequest",
			"X-Token":localStorage.getItem("token")
		},
	   	emulateJSON: false,
	}).then(function(data){
		if(data.data.code==0){
			callback(data.data);
		}else{
			$.toast(data.data.code);
			console.log(data.data);
			self.$router.go("/user.login");
		}
	})
	console.log(this)
	console(2)
}
